from selenium.webdriver import Chrome
from lxml import etree
import re

url = 'https://www.6pm.com/p/lifestride-spark-red/product/8872328/color/585'
d = Chrome()
d.get(url)

# 品牌
brand = d.find_element_by_xpath('//h1[@class="vUkNo"]/span[1]').text

# 名字
title = d.find_element_by_xpath('//h1[@class="vUkNo"]/span[2]').text

# 描述信息
lis = d.find_elements_by_xpath('//div[@class="_1Srfn"]//li')
info = [li.text for li in lis]

# 获取属性
# 
# 通过点击的方式选择下来菜单失败，需要需找另外的方法
# 
# tags = d.find_elements_by_xpath('.//form[@method="POST"]//label/following::*[1]')
# tags = tags[:len(tags)//2]
# tag = tags[0]
# tag.find_element_by_tag_name('select')

# 获取价格信息
# 因为selenium 获取某些元素的文本时不如预期，这里换用lxml的etree
tree = etree.HTML(d.page_source)
off = tree.cssselect('span._3WHDJ')[0].xpath('span/text()')[0]
msrp = tree.xpath('//span[contains(@class, "_1iWd1")]//*/text()')[1]
price = tree.xpath('//span[contains(@class, "_3r_Ou")]/text()')[0]

# 获取库存信息
ok = 'Add to Shopping Bag'
if tree.xpath('//button[@class="_1JFkd"]/text()')[0] == ok:
    stock_text = tree.xpath('//div[contains(@class, "Z_bLp")]/text()')[0]
    stock = int(re.findall('[^0-9]+([0-9]+)', stock_text))
else:
    stock = 0

# 获取图片信息
thumbs = d.find_elements_by_css_selector('#thumbnailsList button')
pic_urls = []
for t in thumbs:
    t.click()	# 点击后大图位置的图片会更新
    d.find_element_by_xpath('//button[contains(@class, "_2PlG6")]').click()
    url = d.find_element_by_xpath('//img[@class="_30ZFS"]').get_attribute('src')
    pic_urls.append(url)
